const ShuoshuoAndPhoto = require('../model/shuoshuoAndPhoto.js');
const ShuoShuo = require('../model/shuoshuo.js');
const { getUserByID } = require('./users.js');
const { Op } = require("sequelize");
const { IMG_URL } = require('../config/getconfig.js')
class Shuoshu {
    /* 创建说说*/
    async createshuoshuo(data) {
        let { shuoshuo_author, shuoshuo_authorid, shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status } = data;
        shuoshuo_status = typeof shuoshuo_status == "string" ? parseInt(shuoshuo_status) : shuoshuo_status
        let result = await ShuoShuo.create({ shuoshuo_author, shuoshuo_authorid, shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status })
        if (result) {
            return {
                code: 1,
                msg: "创建成功",
                data: result.dataValues
            }
        } else {
            return {
                code: 0,
                msg: "创建失败"
            }
        }
    }
    /* 检验说说是否存在通过id */
    async checkShuoshuo(shuoshuo_id) {
        let result = await ShuoShuo.findByPk(shuoshuo_id)
        if (result === null) {
            return {
                code: 0,
                msg: "说说不存在"
            }
        } else {
            return {
                code: 1,
                msg: "说说存在",
            }
        }

    }
    /* 上传说说的图片 */
    async uploadshuoshuoimgname(shuoshuo_id, shuoshuoimgs_name) {
        let list1 = shuoshuoimgs_name.map(item => {
            return {
                shuoshuo_id,
                shuoshuo_url: item
            }
        })
        let res = await ShuoshuoAndPhoto.bulkCreate(list1)
        res = res.map(v => {
            return v.dataValues
        })
        return res.length > 0 ? {
            code: 1,
            msg: "上传成功",
            data: res
        } : {
            code: 0,
            msg: "上传失败"
        }
    }
    /* 获取说说 */
    getshuoshuoByTime = async (page, pageSize, shuoshuo_status) => {
        page = (typeof page == "string") ? parseInt(page) : page;
        pageSize = (typeof pageSize == "string") ? parseInt(pageSize) : pageSize;
        let offset = (page - 1) * pageSize;
        const { count, rows } = await ShuoShuo.findAndCountAll({
            limit: pageSize,
            offset: offset,
            attributes: { exclude: ['updatedAt'] },
            order: [
                ['createdAt', 'DESC']
            ],
            where: {
                shuoshuo_status,
                shuoshuo_istop: 0
            }
        })
        let ress = rows.map(item => item.dataValues)
        if (ress.length > 0) {
            /* 根据说说id获取说说图片 */
            let res = ress.map(async item => {
                let user = await getUserByID(item.shuoshuo_authorid)
                if (user.code) {
                    let isok = await this.getshuoshuoimgByid(item.id)
                    if (isok.length > 0) {
                        return {
                            ...item,
                            avater: user.users.Users_corver,
                            shuoshuoimgs_name: isok
                        }
                    } else {
                        return {
                            ...item,
                            avater: user.users.Users_corver,
                            shuoshuoimgs_name: []
                        }
                    }
                }
            })
            let newres = await Promise.all(res)
            return {
                code: 1,
                msg: "获取成功",
                data: newres,
            }
        } else {
            return {
                code: 0,
                msg: "暂无数据"
            }
        }
    }
    /* 单独获取置顶说说 */
    gettopshuoshuo = async (shuoshuo_istopfleg, shuoshuo_id = null) => {
        let where = {};
        shuoshuo_id && Object.assign(where, { id: shuoshuo_id })
        if (shuoshuo_istopfleg) {
            Object.assign(where, { shuoshuo_istop: 1 })
        }
        let ress = await ShuoShuo.findOne({
            where,
            order: [
                ['createdAt', 'DESC']
            ]
        })
        try {
            if (ress) {
                let user = await getUserByID(ress.dataValues.shuoshuo_authorid)
                if (user.code) {
                    let isok = await this.getshuoshuoimgByid(ress.id)
                    let item = ress.dataValues
                    if (isok.length > 0) {
                        return {
                            code: 1,
                            msg: "获取成功",
                            data: {
                                ...item,
                                avater: user.users.Users_corver,
                                shuoshuoimgs_name: isok
                            }
                        }
                    } else {
                        return {
                            code: 1,
                            msg: "获取成功",
                            data: {
                                ...item,
                                avater: user.users.Users_corver,
                                shuoshuoimgs_name: []
                            }
                        }
                    }
                }
            } else {
                return {
                    code: 0,
                    msg: "获取失败暂无数据"
                }
            }
        } catch (error) {
            console.log(error);
        }
    }
    /* 根据说说id获取说说的图片 */
    async getshuoshuoimgByid(shuoshuo_id) {
        let shuoshuoimgs_name = await ShuoshuoAndPhoto.findAll({
            attributes: { exclude: ['updatedAt', "id", "updataAt", "createdAt"] },
            where: { shuoshuo_id }
        })
        if (shuoshuoimgs_name === null) {
            return null
        } else {
            shuoshuoimgs_name = shuoshuoimgs_name.map(v => v.dataValues)
            return shuoshuoimgs_name
        }
    }
    /* 编辑说说 */
    async edit(data) {
        let { shuoshuo_author, shuoshuo_authorid, shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status, shuoshuo_id } = data;
        shuoshuo_status = typeof shuoshuo_status == "string" ? parseInt(shuoshuo_status) : shuoshuo_status
        let result = await ShuoShuo.update({ shuoshuo_author, shuoshuo_authorid, shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status }
            , {
                where: { id: shuoshuo_id }
            })
        if (result[0] > 0) {
            return {
                code: 1,
                msg: "修改成功",
                data: {
                    shuoshuo_id
                }
            }
        } else {
            return {
                code: 0,
                msg: "修改失败"
            }
        }
    }
    /* 修改说说的图片 */
    async updatashuoshuoimglist(id, imglist) {
        /* 先获取当前说说的所有图片 */
        let oldimgs_name = await ShuoshuoAndPhoto.findAll({
            attributes: { exclude: ['updatedAt', "updataAt", "shuoshuo_id"] },
            where: {
                shuoshuo_id: id
            }
        })
        if (oldimgs_name.length > 0) {
            let oldimglist = oldimgs_name.map(item => item.dataValues.shuoshuo_url)
            let newset = new Set(imglist)
            let oldset = new Set(oldimglist)
            for (let i = 0; i < oldimglist.length; i++) {
                const el = oldimglist[i];
                if (!newset.has(el)) {
                    /* 删除数据库中的图片 */
                    let result = await ShuoshuoAndPhoto.destroy({
                        where: {
                            shuoshuo_url: el
                        }
                    })
                }
            }
            for (let i = 0; i < imglist.length; i++) {
                const el = imglist[i];
                if (!oldset.has(el)) {
                    /* 添加新的图片 */
                    let result = await ShuoshuoAndPhoto.create({
                        shuoshuo_url: el,
                        shuoshuo_id: id
                    })
                }
            }
        }
        return {
            code: 1,
            msg: "修改成功"
        }
    }
    /* 修改封面 */
    async updatacovers(id, shuoshuo_cover, oldshuoshuocover) {
        try {
            let res = await ShuoshuoAndPhoto.update({ shuoshuo_url: (IMG_URL + shuoshuo_cover) }, {
                where: {
                    shuoshuo_url: IMG_URL + oldshuoshuocover,
                    shuoshuo_id: id
                }
            })
            console.log(res, "da");
            if (res[0] > 0) {
                return {
                    code: 1,
                    msg: "修改成功"
                }
            } else {
                return {
                    code: 0,
                    msg: "修改失败"
                }
            }
        } catch (error) {
            console.log(error);
        }
    }
    /* 查看置顶数量 */
    async gettopunm() {
        let res = await ShuoShuo.count({
            where: {
                shuoshuo_istop: 1
            }
        })
        return res;
    }
    /* 置顶于取消置顶 */
    async updatashuoshuoTop(id, istop) {
        let res = await ShuoShuo.update({
            shuoshuo_istop: istop
        }, {
            where: {
                id
            }
        })
        if (res[0] > 0) {
            return {
                code: 1,
                msg: "修改成功"
            }
        } else {
            return {
                code: 0,
                msg: "修改失败"
            }
        }
    }
    /* 修改说说状态 */
    async updatashuoshuostatus(id, status) {
        let res = await ShuoShuo.update({
            shuoshuo_status: status
        }, {
            where: {
                id
            }
        })
        if (res[0] > 0) {
            return {
                code: 1,
                msg: "修改成功"
            }
        } else {
            return {
                code: 0,
                msg: "修改失败"
            }
        }
    }
    /* 删除说说 */
    async deleteshuoshuo(id) {
        let res = await ShuoShuo.destroy({
            where: {
                id
            }
        })
        let ress = await ShuoshuoAndPhoto.destroy({
            where: {
                shuoshuo_id: id
            }
        })
        if (res) {
            return {
                code: 1,
                msg: "删除成功"
            }
        } else {
            return {
                code: 0,
                msg: "删除失败"
            }
        }
    }
    async setlikeshuoshuo(shuoshuo_id, status) {
        let res
        if (status == 1) {
            res = await ShuoShuo.increment("shuoshuo_strs", {
                where: {
                    id:shuoshuo_id,
                }
            })
        } else {
            res = await ShuoShuo.decrement("shuoshuo_strs", {
                where: {
                    id:shuoshuo_id,
                }
            })
        }
        return {
            code: 1,
            msg: "点赞成功",
            data: res
        }
    }

}

module.exports = new Shuoshu();
